// ActionScript file
package   
{   
    import com.wdxc.ImageCut;  
    import com.wdxc.ImageRotation;  
    import flash.geom.Point;  
    import flash.geom.Rectangle;  
    import flash.net.URLLoader;  
    import flash.net.URLRequest;  
    import flash.events.* ;  
    import flash.display.*;  
    import flash.geom.* ;  
    import flash.net.*;  
    import flash.text.TextField;  
    import flash.utils.ByteArray;  
    import com.wdxc.util.* ;        
    import fl.containers.ScrollPane;  
    import flash.display.Shape;  
    import fl.events.ScrollEvent;  
    import flash.external.ExternalInterface;  
    import flash.system.Security;  
    import flash.utils.Timer;  
    import flash.events.TimerEvent ;  
    import com.wdxc.* ;  
      
      
    /**   
     * @author luqinglong 
     */   
    public class Main extends MovieClip  
    {  
        //存储加载进来的图片数据  
        private var _bitmap:Bitmap ;                 
        //进度条  
        private var _progressbar:TextField ;  
          
        private var picname:String ;   
              
        public var _imageCut:ImageCut = new ImageCut() ;  
                 
        //场景引用  
        public static var SCENE:MovieClip ;  
             
        public static var _handerState:int = 0 ;  
                 
              
        public var pelsHint:TextField = new TextField();   
                         
        public var photoid :String = "0";   
           
        public var ir:ImageRotation ;   
              
        /** 
         * 原图和中图的压缩比例   
         */      
        public var picScale :Number = 2 ;  
                 
             
        public function Main():void {             
            flash.system.Security.allowDomain("*");           
              
            photoid = this.loaderInfo.parameters['photoid'];     
                      
            //photoid = "284186" ;      
             
            SCENE = this;             
              
            menu_mc.cut_btn.addEventListener(MouseEvent.MOUSE_DOWN, this._imageCut.startCutPic);  
           
            menu_mc.rotate_btn.addEventListener(MouseEvent.MOUSE_DOWN, rotationHandler) ;    
          
            checkAvailable();     
                    
            this.addPelsHint();       
            startLoadPic();         
        }     
                 
        /** 
         * 加入象素提醒   
         */  
        private function addPelsHint():void {  
            this.addChild(pelsHint);        
            //pelsHint.text = "象素不够" ;  
            pelsHint.x = 150 ;   
            pelsHint.y = 0 ;                        
            pelsHint.height = 20 ;             
            this.pelsHint.width = 350 ;   
            pelsHint.textColor =0xFF0000;  
        }  
            
        public function setPelsHint(p:Number,hint:String):void {  
                                   
            this.pelsHint.text = "像素:" + String(p) + "  " + hint  ;       
                 
        }         
             
        private function rotationHandler(e:MouseEvent):void {      
            ir = new ImageRotation() ;    
            ir.x = menu_mc.x ;  
            ir.y = menu_mc.y ;       
            this.stage.addChild(ir);      
        }     
          
            
        /** 
         * 得到最低象素值 。 
         */  
        /* 
        public function getMaxPels():Number { 
   
            return this.plength/ 500 ; 
        }  */  
            
        //public function setPelsScale(p:Number) {  
            //this.plength = p / 500 ;  
        //}  
                
          
            
        public function set bitmap(bp:Bitmap):void {  
            this._bitmap = bp ;  
        }   
              
        public function get bitmap():Bitmap {  
            return this._bitmap ;  
        }  
                  
        public function startLoadPic():void {  
                               
            var param:URLVariables = new URLVariables() ;   
            var url:String = "/photo/LoaderPhotoServlet";        
            //var url:String = "http://localhost:8080//photo/LoaderPhotoServlet" ;  
                 
            param.random = Math.floor(Math.random() * 1000);                   
            param.photoid = photoid ;             
            //output.text = photoid ;  
            //url += Math.floor(Math.random() * 1000);  
              
                  
            var request:URLRequest = new URLRequest(url);     
            request.data = param ;  
            var loader :Loader = new Loader();  
            loader.load(request);        
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, picLoadCompleteHandler);  
        }   
        /**   
         * 导入二进制完成 
         * @param   e     
         */      
        private function picLoadCompleteHandler(e:Event):void {  
                 
            /**删除进度条*/      
            if (Main._handerState == 1||Main._handerState==2) {  
                 this.delVagueBg();      
                 delLoadingBar();  
                 calljs();  
            }             
            Main._handerState = 0 ;  
                
            /* 
            if (this._bitmap.bitmapData != null) { 
                 
                this._bitmap.bitmapData.dispose() ;    
                //this.removeChild(_bitmap); 
            } */            
              
            this._bitmap =  new Bitmap() ;            
                       
            var loader:Loader = e.target.loader as Loader;          
                
            var _bitmapdata:BitmapData = new BitmapData(loader.width, loader.height);  
            _bitmapdata.draw(loader);   
                  
                   
            /** 
             * 判断横版照片，竖版照片 
             */   
            /* 
            if ( loader.height > loader.width) { 
                //竖版照片   
                this.cutscaleFlag = Constant.VERTICAL ;     
            } */    
                 
                    
                                  
            this._bitmap.bitmapData = _bitmapdata;                 
            this.addChild(_bitmap);    
            this.reset() ;         
            //设置原图引用                   
            this._imageCut.bitmap = this._bitmap ;                     
            this.addChild(this._imageCut);  
                 
            e.currentTarget.removeEventListener(Event.COMPLETE, picLoadCompleteHandler);   
                    
        }       
            
             
          
        /**  
         * 删除以前的Bitmapt对象，注入新的进去 。 
         */  
        public function removeBitmap():void {  
                       
            this.removeChild(_bitmap);  
        }          
        public function reset():void {    
            this._bitmap.x = (Constant.LEFT_AREA_WIDTH-_bitmap.width)/2 ;  
            this._bitmap.y = (Constant.SCRENE_HEIGHT - _bitmap.height) / 2 ;  
        }  
          
        public function checkAvailable():void {  
            if (ExternalInterface.available){  
                try {      
                    ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);  
                    if (checkJavaScriptReady()) {  
                        //trace("JavaScript is ready.\n");  
                    } else {   
                                      
                        //trace("JavaScript is not ready, creating timer.\n");  
                        var readyTimer:Timer = new Timer(100, 0);  
                        readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);  
                        readyTimer.start();         
                    }  
                }catch (error:SecurityError) {  
                    //trace("A SecurityError occurred: " + error.message + "\n");  
                    //output.text = "error1";  
                }catch (error:Error) {    
                      
                    //trace("An Error occurred: " + error.message + "\n");  
                }     
            }else{     
                //trace("External interface is not available for this container.");  
            }  
           
        }   
           
        private function timerHandler(event:TimerEvent):void {  
             
            var isReady:Boolean = checkJavaScriptReady();   
            //output.appendText("123");  
            if (isReady) {                
                //trace("JavaScript is ready.\n");   
                Timer(event.target).stop();  
            }  
        }  
                    
                                
        private function receivedFromJavaScript(pid:String, plength:Number):void {         
                              
            //pelsHint.text = pid + "==" + plength ;  
            this.photoid = pid ;        
                
            //设置中图和原图的像素比  
            this.picScale = plength/500 ;       
                
            //this._bitmap.bitmapData.dispose();  
            //output.text = value ;        
            removeBitmap();          
            // 点击了裁剪按钮时  
            if (Main._handerState == 5) {  
                _imageCut.removeCutMenu();      
                   
            }      
            // 裁剪时候单击保存时候   
            else if (Main._handerState == 6) {  
                _imageCut.removeCutMenu();      
                _imageCut.removeHint();                
            } else if (Main._handerState == 10) {  
                ir.remove();  
            }    
              
       
              
              
            //this._bitmap = null ;  
            startLoadPic();          
        }  
        private function checkJavaScriptReady():Boolean {  
            var isReady:Boolean = ExternalInterface.call("isReady");  
            return isReady;  
        }   
                    
        public function calljs():void {  
            if (ExternalInterface.available) {       
                ExternalInterface.call("rationAndCropComplete", "123");  
            }       
        }  
            
           
        public var  _vaguebg:Sprite ;  
        public function addVagueBg():void {   
            _vaguebg = new Sprite() ;            
            _vaguebg.graphics.beginFill(0x000000);  
            _vaguebg.graphics.drawRect(0, 0, 770, 530);  
            _vaguebg.graphics.endFill();  
            _vaguebg.alpha = 0.5;                     
                              
            this.addChild(_vaguebg);      
                  
        }       
        public function delVagueBg():void {    
            this.removeChild(_vaguebg);  
        }        
          
          
        public var loadingbar:Sprite ;    
        public function addLoadingBar():void {  
            loadingbar = new LoadingBar();                   
            this.addChild(loadingbar) ;      
            loadingbar.x = 200 ;  
            loadingbar.y = 150 ;  
        }      
                
        public function delLoadingBar():void {  
            this.removeChild(loadingbar);  
        }  
    }     
}